﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExifTool;

namespace ImgPosWriter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("图像坐标填写工具V1.0.0.0");
            Console.WriteLine();
            Console.ResetColor();

            string imgFolder = AppDomain.CurrentDomain.BaseDirectory;
            string[] imgs = GetImagesInFolder(imgFolder);

            Console.WriteLine($"当前目录下图像共 {imgs.Length} 张");


            string csvFile = Path.Combine(imgFolder, "pos.csv");
            if (File.Exists(csvFile))
            {
                string posfile = csvFile;
                PosParser parser = new PosParser();
                Pos[] poses = parser.Parse(posfile).ToArray();
                Console.WriteLine($"pos文件内pos共 {poses.Length} 个");

                int count = new int[] {poses.Length, imgs.Length}.Min();
                Console.WriteLine($"会对前 {count} 个图像执行填写坐标");
                Console.WriteLine();
                Console.WriteLine($"正在执行，请稍后......");

                for (int i = 0; i < count; i++)
                {
                    ExifUtil.WriteCoord(imgs[i],poses[i].Long,poses[i].Lat,poses[i].Height);
                }
                Console.WriteLine($"执行完毕");
            }
            else
            {
                Console.WriteLine($"当前目录下图像没有pos文件（pos.csv），无法继续执行");
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"按任意键退出");
            Console.ResetColor();
            Console.ReadKey();


        }


        private static string[] GetImagesInFolder(string imgFolder)
        {
            ///获得数据集目录下所有图像
            var exts = new List<string>() { ".jpg", ".jpeg", ".png" };
            string[] imageFiles = Directory.GetFiles(imgFolder).Where(i => exts.Contains(Path.GetExtension(i).ToLower())).ToArray();
            return imageFiles;
        }
    }
}
